package vn.gq.udv.comic.fragment;

import java.io.IOException;
import java.util.ArrayList;
import java.util.zip.GZIPInputStream;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONTokener;

import com.google.ads.AdView;

import vn.gq.udv.comic.BaseActivity;
import vn.gq.udv.comic.BookmarkHelper;
import vn.gq.udv.comic.R;
import vn.gq.udv.comic.ReaderActivity;
import vn.gq.udv.comic.model.ChapterAdapter;
import vn.gq.udv.comic.model.item.Bookmark;
import vn.gq.udv.comic.model.item.ChapterItem;
import vn.gq.udv.tab.BaseFragment;
import vn.gq.udv.utils.AdmobAdsHandler;
import vn.gq.udv.utils.DialogUtils;
import vn.gq.udv.utils.IOUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.ProgressBar;

public class ChapterListFragment extends BaseFragment implements OnItemClickListener {

	private ListView mListView;
	private ChapterAdapter mAdapter;
	private ProgressBar mProgressBar;
	private AdmobAdsHandler mAdHandler;
	
	public static ChapterListFragment newInstance() {
		ChapterListFragment f = new ChapterListFragment();
		return f;
	}
	
	@Override
	public int getContainerId() {
		return 0;
	}

	@Override
	public int getLayoutId() {
		return R.layout.fragment_chapter_scr;
	}

	@Override
	public void onResume() {
		super.onResume();
		mAdHandler.loadAds();
	}

	public void showPopUpBookmark() {
		try {
			BookmarkHelper bookmarkHelper = BookmarkHelper.newInstance(getActivity());
			
			if (bookmarkHelper.hasBookmark()) {
				
				final Bookmark bookmark = bookmarkHelper.loadBookmark();
				
				final ChapterItem chapterItem = new ChapterItem(bookmark.getChapterId(), bookmark.getTitle());
				
				String msg = String.format("Bạn có muốn đọc tiếp Trang %d \"%s\"", bookmark.getPosition()+1, bookmark.getTitle());
				
				DialogUtils.newInstance(getActivity()).showConfirm(msg, new DialogUtils.OnDialogConfirmListener() {
					
					@Override
					public void onOk() {
						startChapter(chapterItem, bookmark.getPosition());
					}
					
					@Override
					public void onCancel() {
						
					}
				});
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void createView() {
		mAdHandler = new AdmobAdsHandler((AdView) mLayout.findViewById(R.id.adView));
		mProgressBar = (ProgressBar) mLayout.findViewById(R.id.loading);
		mListView = (ListView) mLayout.findViewById(android.R.id.list);
		mListView.setOnItemClickListener(this);
		mAdapter = new ChapterAdapter(getActivity());
		mAdapter.load(new Runnable() {
			
			@Override
			public void run() {
				mListView.setAdapter(mAdapter);
				mProgressBar.setVisibility(View.GONE);
				mListView.setVisibility(View.VISIBLE);
				
//				((BaseActivity) getActivity()).mHandler.postDelayed(new Runnable() {
//					
//					@Override
//					public void run() {
//						showPopUpBookmark();
//					}
//				}, 2000);
			}
		});
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		if (id > -1) {
			final ChapterItem chapterItem = (ChapterItem) mAdapter.getItem(position);
			BookmarkHelper.newInstance(getActivity()).saveBookmark(chapterItem, position);
			startChapter(chapterItem, 0);
		}
	}
	
	public void startChapter(final ChapterItem chapterItem, final int position) {
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				String json;
				try {
					json = IOUtils.convertStreamToString(new GZIPInputStream(getActivity().getAssets().open(String.format("data/%s", chapterItem.getChapterId()))));
					JSONArray imageArr = (JSONArray) new JSONTokener(json).nextValue();
					ArrayList<String> imagesData = new ArrayList<String>();
					for (int i=0;i<imageArr.length();i++) {
						imagesData.add(imageArr.getString(i));
					}
					ReaderActivity.startActivity(getActivity(), chapterItem, imagesData, position);
				} catch (IOException e) {
					e.printStackTrace();
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		}).run();
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		mAdHandler.onDestroy();
	}
}
